package com.djhu.freepartner.web.service.impl;

import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.core.RestRsp;
import com.djhu.freepartner.core.Status;
import com.djhu.freepartner.core.exception.ServiceException;
import com.djhu.freepartner.service.IPartnerUserService;
import com.djhu.freepartner.utils.IdcardValidator;
import com.djhu.freepartner.utils.RegUtils;
import com.djhu.freepartner.web.entity.enums.CertificationStatus;
import com.djhu.freepartner.web.service.BonuspointsService;
import com.djhu.freepartner.web.service.UserService;
import com.djhu.freepartner.web.service.model.UserValidModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private BonuspointsService bonuspointsService;

    @Autowired
    private IPartnerUserService partnerUserService;

    @Override
    public RestRsp validstatus(UserValidModel validModel) {
        RestRsp restRsp = new RestRsp();
        String userid = validModel.getUserid();
        PartnerUser user = partnerUserService.selectById(userid);
        if (user == null){
            log.info("user is not exist,user id is {}",userid);
            restRsp.setMsg("用户不存在");
            restRsp.setCode(Status.ACCOUNT_NOT_FOUND.getCode());
            return restRsp;
        }

        int certificationstatus = user.getCertificationstatus();
        if (certificationstatus == CertificationStatus.YES.getCode()){
            restRsp.setMsg("用户已经实名认证");
            restRsp.setCode(Status.ALREADY_EXIST.getCode());
            return restRsp;
        }else if (certificationstatus == CertificationStatus.NO.getCode()){
            boolean match = RegUtils.match(validModel.getName(),RegUtils.NAME);
            match = match && IdcardValidator.isValidate18Idcard(validModel.getIdcard());
            if (match){
                restRsp.setMsg("用户实名认证成功");
                restRsp.setCode(Status.SUCCESS.getCode());
                return restRsp;
            }else {
                restRsp.setMsg("用户实名认证 失败");
                restRsp.setCode(Status.NO_AUTHENTICATION.getCode());
                return restRsp;
            }

        }else {
            restRsp.setMsg("无效状态");
            restRsp.setCode(Status.SERVER_ERROR.getCode());
            return restRsp;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestRsp valid(String userid, String idcard, String name) throws ServiceException {
        PartnerUser partnerUser = new PartnerUser();
        partnerUser.setUserid(userid);
        partnerUser.setIdcard(idcard);
        partnerUser.setName(name);
        partnerUser.setIdcardverifi(CertificationStatus.YES.getCode());
        if (partnerUserService.updateById(partnerUser)) {
            bonuspointsService.recharge(userid, 50, "实名认证 增加积分");
        }
        RestRsp restRsp = new RestRsp();
        restRsp.setCode(Status.SUCCESS.getCode());
        return restRsp;
    }
}
